package com.example.gildestudy1;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yuwei on 2016/11/24.
 */
public class RecyclerViewActivity extends AppCompatActivity {
    private RecyclerView recyclerView ;
    private MyAdapter myAdapter;
    private SwipeRefreshLayout swipeRefreshLayout;
    private int lastVisibleItem;
    private List<String> lists = new ArrayList<String>();
    public final static String[] date = new String[]{"https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3205417797,1731141045&fm=80&w=179&h=119&img.JPEG",
            "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2927138023,1676800987&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3191011590,1726508056&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3205417797,1731141045&fm=80&w=179&h=119&img.JPEG",
            "https://ss1.baidu.com/6ONXsjip0QIZ8tyhnq/it/u=2927138023,1676800987&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3191011590,1726508056&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3191011590,1726508056&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3205417797,1731141045&fm=80&w=179&h=119&img.JPEG",
            "https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png",
            "https://ss0.baidu.com/6ONWsjip0QIZ8tyhnq/it/u=3205417797,1731141045&fm=80&w=179&h=119&img.JPEG"};
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recyclerview);
        //设置隐藏标题栏
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(option);
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();


        getData();
        recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipeRefreshLayout.setProgressViewOffset(false, 0, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
                        .getDisplayMetrics()));
        swipeSwipe();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(RecyclerViewActivity.this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerViewScrollClick();
        myAdapter = new MyAdapter(RecyclerViewActivity.this ,lists );
        recyclerView.setAdapter(myAdapter);
    }

    private void recyclerViewScrollClick() {
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState ==RecyclerView.SCROLL_STATE_DRAGGING && lastVisibleItem + 1 == myAdapter.getItemCount()) {
                    myAdapter.changeMoreStatus(MyAdapter.LOADING_MORE);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            List<String> imlists = new ArrayList<String>();
                            for (int i = 0; i< 10; i++) {
                                imlists.add(date[0]);
                            }
                            myAdapter.add(imlists);
                            myAdapter.changeMoreStatus(MyAdapter.PULLUP_LOAD_MORE);
                        }
                    }, 2500);
                }
            }
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView,dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                lastVisibleItem = layoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private void swipeSwipe() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Log.d("zttjiangqq","invoke onRefresh...");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        List<String> imlists = new ArrayList<String>();
                        for (int i = 0; i <10; i++) {
                            int index = i + 1;
                            imlists.add(date[0]);
                        }
                        myAdapter.add(imlists);
                        swipeRefreshLayout.setRefreshing(false);
                        Toast.makeText(RecyclerViewActivity.this, "更新了五条数据...", Toast.LENGTH_SHORT).show();
                    }
                }, 5000);
            }
        });
    }

    private void getData() {
        for (int i=0;i<date.length;i++){
            lists.add(date[i]);
        }
    }
}
